package com.fowo.api.common.excel;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ImageData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.sys.service.FileStorageService;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Excel文件类型转换器
 *
 * @author
 */
@Slf4j
public class ImageConvert implements Converter<ArrayList<SysFile>> {

    @Override
    public Class<?> supportJavaTypeKey() {
        return ArrayList.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.EMPTY;
    }

    /**
     * 图片转换
     * https://easyexcel.opensource.alibaba.com/docs/current/quickstart/write#%E5%9B%BE%E7%89%87%E5%AF%BC%E5%87%BA
     *
     * @param list
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws IOException
     */
    @Override
    public WriteCellData<?> convertToExcelData(ArrayList<SysFile> list, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) throws IOException {
        //返回空白
        if (CollUtil.isEmpty(list)) {
            return new WriteCellData<>("");
        }
        WriteCellData<Void> writeCellData = new WriteCellData<>();
        // 这里可以设置为 EMPTY 则代表不需要其他数据了
        writeCellData.setType(CellDataTypeEnum.STRING);
        writeCellData.setStringValue("");
        try {
            // 可以放入多个图片
            List<ImageData> imageDataList = CollUtil.newArrayList();
            for (SysFile sysFile : list) {
                if (ObjectUtil.isNotEmpty(sysFile) && StrUtil.containsAnyIgnoreCase(sysFile.getMimeType(), "image/jpeg", "image/png", "image/jpeg", "image", "jpg", "png", "jpg")) {
                    ImageData imageData = new ImageData();
                    // 放入进制图片
                    imageData.setImage(imageFactory(sysFile));
                    imageDataList.add(imageData);
                }
            }
            writeCellData.setImageDataList(imageDataList);
            if (writeCellData == null) {
                return new WriteCellData<>("");
            }
        } catch (Exception e) {
            return new WriteCellData<>("图片异常:" + ExceptionUtil.getRootCauseMessage(e));
        }
        // 生成 Excel 小表格
        return writeCellData;
    }

    /**
     * 获取图片流
     *
     * @param sysFile
     * @return
     * @throws Exception
     */
    private byte[] imageFactory(SysFile sysFile) throws Exception {
        FileStorageService oss = SpringUtil.getBean(sysFile.getStorageType());
        InputStream read = oss.read(sysFile.getStorageKey());
        //开启压缩
        // this.insertImage(sheet, cell, imageDataList.get(0).getImage(), 0);
        //ByteArrayOutputStream thumbnailBaos = new ByteArrayOutputStream();
        //thumbnailService.createThumbnail(50,50,read,thumbnailBaos);
        //byte[] bytes = thumbnailBaos.toByteArray();
        byte[] bytes = IoUtil.readBytes(read);
        return bytes;
    }
}
